package io.renren.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.renren.entity.CurrentGroup;
import io.renren.entity.Index;
import io.renren.service.IndexService;
import io.renren.utils.R;
@Controller
@RequestMapping("map_index")
public class IndexController {
	@Autowired
	private IndexService indexService;
	private Logger logger = LoggerFactory.getLogger(getClass());
	@ResponseBody
	@RequestMapping("/list")
	public R list(){
		 Map<String,Object> map = new HashMap<>();  

	          map.put("id", 1);
	          List<Index> indexlist =indexService.getCurrentLocationList(map); 
	          int totalcount=indexlist.size();
	          Map<String, List<Index>> groups = indexlist.stream().collect(Collectors.groupingBy(Index::getGroupName));
	          List<CurrentGroup> listgroup=new ArrayList<>();
	          groups.forEach((k, v) ->{
	        	  CurrentGroup group=new CurrentGroup();
	        	  group.setGroupName(k);
	        	  group.setColor(v.get(0).getRgb());
	        	  group.setCount(v.size());
	        
	        	  listgroup.add(group);
	          });   
	        
	          listgroup.forEach(g->logger.info("{}",g.getGroupName()));
		return R.ok().put("indexlist",indexlist).put("listgroup", listgroup).put("totalcount",totalcount);
	}
	//越界报警列表
	@ResponseBody
	@RequestMapping("/crosslist")
	public R crosslist(){
		
	          List<Index> crosslist =indexService.getCurrentCrossList();  
		return R.ok().put("crosslist",crosslist);
	}
	//离岗报警列表
	@ResponseBody
	@RequestMapping("/dislist")
	public R dislist(){
		
		List<Index> dislist =indexService.getCurrentDisList(); 
		System.out.println(dislist+"-------------------");
		return R.ok().put("dislist",dislist);
	}
	
}
